//
//  CZStatusPictureView.swift
//  Weibo28
//
//  Created by Apple on 16/5/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//  重用标记
private let CZStatusPictureViewCellIdentifier = "CZStatusPictureViewCellIdentifier"
//  每项之间的间距
let itemMargin: CGFloat = 5
//  计算每项图片的一个宽度
let itemWidth = (ScreenWidth - 2 * StatusTableViewCellMargin - 2 * itemMargin) / 3


//  配图视图
class CZStatusPictureView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {

    //  绑定数据模型
    var picUrls: [CZStatusPictureInfo]? {
        didSet {
            messageLabel.text = "\(picUrls?.count ?? 0)"
            
            let currentSize = calcSize(picUrls?.count ?? 0)
            //  根据配图个数生成一个CGSize, 设置大小约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(currentSize)
            }
            //  重新加载数据
            reloadData()
            
        }
    }
    
    //  MARK:   -懒加载
    
    private lazy var messageLabel: UILabel = UILabel(textColor: UIColor.redColor(), fontSize: 20)
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        //  设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //  设置间距
        //  设置水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        //  设置垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = RandomColor()
        addSubview(messageLabel)
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        //  注册cell
        registerClass(CZStatusPictureViewCell.self, forCellWithReuseIdentifier: CZStatusPictureViewCellIdentifier)
        //  设置数据源代理
        dataSource = self
        //  设置代理
        delegate = self
        
    }
    
    //  根据图片数量生成CGSize
    private func calcSize(count: Int) -> CGSize {
       
        //  列数怎么计算
        let cols = count > 3 ? 3 : count
        
        //  行数怎么计算 需要记住
        let rows = (count - 1) / 3 + 1
        //  当前视图的宽度
        let currentWidth = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        //  当前视图的高度
        let currentHeight = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        
        return CGSize(width: currentWidth, height: currentHeight)
        
    }
    
    //  MARK:   UICollectionViewDataSource 数据源代理方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZStatusPictureViewCellIdentifier, forIndexPath: indexPath) as! CZStatusPictureViewCell
        //  设置数据
        cell.picInfo = picUrls![indexPath.item]
        return cell
    }
    
    //  MARK:   UICollectionViewDelegate 代理方法
//    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//    {
//    SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new];
//    photoBrowser.delegate = self;
//    photoBrowser.currentImageIndex = indexPath.item;
//    photoBrowser.imageCount = self.modelsArray.count;
//    photoBrowser.sourceImagesContainerView = self.collectionView;
//    
//    [photoBrowser show];
//    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let photoBrowser = SDPhotoBrowser()
        photoBrowser.delegate = self
        photoBrowser.currentImageIndex = indexPath.item
        photoBrowser.imageCount = picUrls?.count ?? 0
        //  来源视图
        photoBrowser.sourceImagesContainerView = self
        photoBrowser.show()
        
    }
}

extension CZStatusPictureView: SDPhotoBrowserDelegate {
    
    
    

//    // 返回临时占位图片（即原来的小图）
//    - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
//    {
//    // 不建议用此种方式获取小图，这里只是为了简单实现展示而已
//    SDCollectionViewDemoCell *cell = (SDCollectionViewDemoCell *)[self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
//    
//    return cell.imageView.image;
//    
//    }
//    
//    
//    // 返回高质量图片的url
//    - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
//    {
//    NSString *urlStr = [[self.modelsArray[index] thumbnail_pic] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
//    return [NSURL URLWithString:urlStr];
//    }
    
    // 返回临时占位图片（即原来的小图）
    func photoBrowser(browser: SDPhotoBrowser!, placeholderImageForIndex index: Int) -> UIImage! {
        let indexPath = NSIndexPath(forItem: index, inSection: 0)
        let cell = self.cellForItemAtIndexPath(indexPath)! as! CZStatusPictureViewCell
        
        return cell.imageView.image
    }
    // 返回高质量图片的url
    func photoBrowser(browser: SDPhotoBrowser!, highQualityImageURLForIndex index: Int) -> NSURL! {
        let urlStr = picUrls![index].thumbnail_pic!.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
        return NSURL(string: urlStr)
    }

}



//  自定义配图cell
class CZStatusPictureViewCell: UICollectionViewCell {
    
    //  设置模型
    var picInfo: CZStatusPictureInfo? {
        didSet {
            
            guard let url = picInfo?.thumbnail_pic else {
                return
            }
            
           
            imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
//            if url.hasSuffix(".gif") {
//                gifImageView.hidden = false
//            } else {
//                gifImageView.hidden = true
//            }
            gifImageView.hidden = !url.hasSuffix(".gif")
            
            
            
        }
    }
    //  图片
    private lazy var imageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        //  设置图片等比填充
        view.contentMode = UIViewContentMode.ScaleAspectFill
        //  多余部分截取掉
        view.clipsToBounds = true
        return view
    }()
    //  gif图片
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
        
    }
    
}












